package com.example.admin.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentChats mChat;
    private FragmentContacts mContact;
    private FragmentDiscover mDiscover;
    private FragmentMe mMe;

    private LinearLayout mTBChat;
    private LinearLayout mTBContact;
    private LinearLayout mTBDiscover;
    private LinearLayout mTBMe;

    private FragmentManager fragmentManager;

    TextView t;

    private void initViews() {
        mTBChat = (LinearLayout)findViewById(R.id.ll_Chat);
        mTBContact = (LinearLayout)findViewById(R.id.ll_Contact);
        mTBDiscover = (LinearLayout)findViewById(R.id.ll_Ds);
        mTBMe = (LinearLayout)findViewById(R.id.ll_Me);

        mTBChat.setOnClickListener(this);
        mTBContact.setOnClickListener(this);
        mTBDiscover.setOnClickListener(this);
        mTBMe.setOnClickListener(this);

    }

    private void  hideFragments(FragmentTransaction transaction) {
        if (mChat != null) {
            transaction.hide(mChat);
        }
        if (mContact!= null) {
            transaction.hide(mContact);
        }
        if (mDiscover != null) {
            transaction.hide(mDiscover);
        }
        if (mMe != null) {
            transaction.hide(mMe);
        }
    }

    private void resetBtn()
    {
        ((ImageView) mTBChat.findViewById(R.id.iv_Chat))
                .setImageResource(R.drawable.chat1);
        ((ImageView) mTBContact.findViewById(R.id.iv_Contact))
                .setImageResource(R.drawable.contact1);
        ((ImageView) mTBDiscover.findViewById(R.id.iv_Ds))
                .setImageResource(R.drawable.discover1);
        ((ImageView) mTBMe.findViewById(R.id.iv_Me))
                .setImageResource(R.drawable.me1);

    }


    private void setTabsSelection(int index) {

        resetBtn();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                ((ImageView) mTBChat.findViewById(R.id.iv_Chat)).setImageResource(R.drawable.chat);
                if (mChat == null) {
                    mChat = new  FragmentChats();
                    transaction.add(R.id.frame_content, mChat);
                }else {
                    transaction.show(mChat);
                }
                break;
            case 1:
                ((ImageView) mTBContact.findViewById(R.id.iv_Contact)).setImageResource(R.drawable.contact);
                if (mContact == null) {
                    mContact = new FragmentContacts();
                    transaction.add(R.id.frame_content, mContact);
                }else {
                    transaction.show(mContact);
                }
                break;
            case 2:
                ((ImageView) mTBDiscover.findViewById(R.id.iv_Ds)).setImageResource(R.drawable.discover);
                if (mDiscover == null) {
                    mDiscover = new FragmentDiscover();
                    transaction.add(R.id.frame_content, mDiscover);
                }else {
                    transaction.show(mDiscover);
                }
                break;
            case 3:
                ((ImageView) mTBMe.findViewById(R.id.iv_Me))
                        .setImageResource(R.drawable.me);
                if (mMe == null) {
                    mMe = new FragmentMe();
                    transaction.add(R.id.frame_content, mMe);
                }else {
                    transaction.show(mMe);
                }
                break;
        }transaction.commit();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        fragmentManager = getSupportFragmentManager();
        setTabsSelection(0);
        /*initImge();
        ImgeAdapter adapter = new ImgeAdapter(MainActivity.this, R.layout.imge_main, imgeList);
        ListView listView = (ListView) findViewById(R.id.list_view_chats);
        listView.setAdapter(adapter);*/

    }

   /* private void initImge() {
        for (int i = 0; i < 5; i++) {
            imgeList.add(new Imge("Apple", R.drawable.chat));
        }
    }*/



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_Chat:
                setTabsSelection(0);
                break;
            case R.id.ll_Contact:
                setTabsSelection(1);
                break;
            case R.id.ll_Ds:
                setTabsSelection(2);
                break;
            case R.id.ll_Me:
                setTabsSelection(3);
                break;
             default:
                 break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
